package com.azunitech.jms.repos;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.ejb.Ejb3Configuration;

import com.azunitech.jms.common.IRepository;

/**
 * Class used to read the project version from a maven filtered resource bundle. 
 */
public abstract class DefaultRepository implements IRepository{
	protected EntityManager m_EntityManager;

	public void init(){
        Ejb3Configuration cfg = new Ejb3Configuration();  
        cfg.setProperty( "hibernate.connection.driver_class", "org.hsqldb.jdbcDriver" );  
        cfg.setProperty( "hibernate.connection.username", "sa" );  
        cfg.setProperty( "hibernate.connection.password", "" );  
        cfg.setProperty( "hibernate.connection.url", "jdbc:hsqldb:mem:test" );  
        cfg.setProperty( "hibernate.dialect", "org.hibernate.dialect.HSQLDialect" );  
        cfg.setProperty( "hibernate.hbm2ddl.auto", "create-drop" );
        
        List<Class<?>> clzs = AnnotatedClasses();
        
        for( Class<?> clz : clzs){
        	cfg.addAnnotatedClass(clz);
        }
        m_EntityManager = cfg.buildEntityManagerFactory().createEntityManager();  	
        getService(m_EntityManager);
	}
	
	protected abstract List<Class<?>> AnnotatedClasses();
	protected abstract ServiceBase getService( EntityManager em );
	
	public void close() {
		this.m_EntityManager.clear();
		this.m_EntityManager.close();
	}
}
